<template>
  <div class="login-wrap" :style="{backgroundImage:'url(static/img/login_bg.png)'}">
    <div class="content">
        <div class="login-box">
            <div class="loginTop">
                <img src="static/img/loginLogo.png" alt="logo">
                <span class="title">人员定位系统</span>
            </div>
            <label class="input-wrapper">
                <input type="text" class="input-css" placeholder="登陆账号" v-model="userName">
            </label>
            <label class="input-wrapper">
                <input type="password" class="input-css" placeholder="密码" v-model="userPwd" onpaste="return false">
            </label>
            <label class="input-wrapper input-code">
                <input type="text" class="input-css" placeholder="输入图形码" v-model="graphicCode">
                <div @click="changeCode" class="identify">
                    <Identify :identifyCode="identifyCode"></Identify>
                </div>
            </label>
            <label class="input-wrapper input-code">
                <input type="text" class="input-css" placeholder="输入短信验证码" v-model="msgCode" maxlength="6">
                <div class="mesCode">
                    <button @click="getCode" :disabled="disabled" :class="{'unChoose': disabled}">{{btnTitle}}</button>
                </div>
            </label>
            <div class="agreement">
                <span class="choose" @click="choosed = !choosed">
                    <i v-show="choosed" class="el-icon-check"></i>
                </span>
                <span class="loginAgreement">登录即同意《服务协议》、《隐私条款》、《中国移动号码服务条款》</span>
            </div>
            <button type="submit" @click="login" class="login-btn">登录</button>
            <button type="submit" @click="forgetPassword" class="login-forget">忘记密码</button>
        </div>
    </div>
    <div class="bottom">
        <div>江苏移动版权所有 苏ICP备11070397号-933 copyright ©</div>
        <div>江苏移动和教育 中国移动通信集团江苏有限公司 ALL Rights Reserved 版权所有</div>
    </div>
  </div>
</template>


<script>
import Storage from '@/base/storage.js'
import Identify from 'components/Identify.vue'

export default {
    components: { Identify },
    data(){
        return {
            userName:'', //账号
            userPwd:'', //密码
            identifyCode: '1234', // 验证码初始值
            identifyCodes: '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',  // 验证码的随机取值范围
            btnTitle: "获取验证码",
            graphicCode: "",//图形码
            disabled:false,  //是否可点击
            choosed: false,
            msgCode: ""
        };
    },
    methods:{
        login(){
            if(this.choosed && (this.identifyCode.toLowerCase() == this.graphicCode.toLowerCase())) {
                let url = `/lbs/usercenter/login/h5JudgeCode`;
                let params = {
                    userAcct: this.userName,
                    userPassword: this.userPwd,
                    operType: "DL",
                    code: this.msgCode
                }
                this.$http.post(url, params).then(res => {
                    let {retCode,retMsg,data} = res.data;
                    if(retCode == '0') {
                        this.$http.defaults.headers.tokenid = data.token;
                        Storage.session.set('operatorId', data.userId);
                        Storage.session.set('token', data.token);
                        this.$http.post('/lbs/usercenter/menu/qryRoleFunctionByUserId').then(res => {
                            let {retCode,retMsg,data} = res.data;
                            if (retCode == '0') {//成功的情况
                                Storage.session.set('funArr', data);
                                console.log(data);
                                this.$router.push('/main');
                            } else {
                                this.$alert(retMsg || '请求失败');
                            }
                        }).catch(err => {
                          this.$alert(err)
                        })
                    }else {
                        this.$message({
                            message: retMsg || "登陆失败",
                            type: 'warning'
                        });
                    }
                })
            }else if(this.userName.trim() == '' || this.userPwd.trim() == ''){
                this.$message({
                    message: "请输入账号和密码",
                    type: 'warning'
                });
            }else if(this.graphicCode == '' || (this.identifyCode.toLowerCase() != this.graphicCode.toLowerCase())) {
                this.$message({
                    message: "请输入正确的图形码",
                    type: 'warning'
                });
            }else if(this.msgCode.trim() == '') {
                this.$message({
                    message: "请输入短信验证码",
                    type: 'warning'
                });
            }else if(!this.choosed) {
                this.$message({
                    message: "请同意服务协议",
                    type: 'warning'
                });
            }
        },
        //获取随机数
        randomNum(min, max) {
            return Math.floor(Math.random() * (max - min) + min);
        },
        // 随机生成验证码字符串
        makeCode () {
            for (let i = 0; i < 4; i++) {
                this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length - 1)]
            }
        },
        // 点击验证码刷新验证码
        changeCode () {
            this.identifyCode = ''
            this.makeCode();
        },
        //获取验证码
        getCode() {
            this.disabled = true;
            if(this.identifyCode.toLowerCase() == this.graphicCode.toLowerCase()) {
                let url = "/lbs/usercenter/login/h5SendCode";
                let params = {
                    userAcct: this.userName,
                    userPassword: this.userPwd,
                    operType: "DL",
                    unLoadFlg: true
                }
                this.$http.post(url, params).then(res => {
                    if(res.data.retCode == "0") {
                        this.$message({
                            message: '验证码已发送',
                            type: 'success'
                        });
                        //倒计时
                        let time = 60;
                        let timer = setInterval(() => {
                            if(time == 0) {
                                clearInterval(timer);
                                this.disabled = false;
                                this.btnTitle = "获取验证码";
                            } else {
                                this.btnTitle =time + '秒后重试';
                                this.disabled = true;
                                time--
                            }
                        },1000)
                    }else {
                        this.disabled = false;
                        this.$message({
                            message: res.data.retMsg,
                            type: 'warning'
                        });
                    }
                })
                .catch(response => {
                    this.disabled = false;
                    this.$message({
                        message: response,
                        type: 'error'
                    });
                })
            }else if(this.graphicCode == ""){
                this.disabled = false;
                this.$message({
                    message: "请输入图形码",
                    type: 'warning'
                });
            }else {
                this.disabled = false;
                this.$message({
                    message: "请输入正确图形码",
                    type: 'warning'
                });
                this.identifyCode = '';
                this.makeCode();
            }
        },
        forgetPassword() {
            this.$router.push("forgetPassword")
        }
    },
    mounted() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
    }
}
</script>

<style scoped lang="less">
button {
    outline: none;
}
.login-wrap{
    position: absolute;
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
    text-align: center;
}
input {
    outline: none;
}
.content {
    width: 400px;
    border:1px solid #ccc;
    border-radius: 16px;
    background-color: #fff;
    box-shadow: 4px 4px 8px #ccc;
    position: absolute;
    left: 60%;
}
.login-box{
    flex: 1;
    padding: 36px;
    .input-wrapper {
        display: flex;
        font-size: 14px;
        width: 100%;
        height: 48px;
        padding: 0;
        color: #8590a6;
        border: none;
        border-bottom: 1px solid #ebebeb;
        border-radius: 0;
    .input-css{
        flex: 1 1;
        padding: 0 16px;
        overflow: hidden;
        background: transparent;
        border: none;
        resize: none;
        color: #1a1a1a;
    }
}
.input-code {
    border-bottom: 0;
    display: flex;
    align-items: center;
    height: 52px;
    justify-content: space-between;
    margin-top: 5px;
    .input-css{
        flex: 0.9;
        padding: 0 16px;
        overflow: hidden;
        background: #F6F6F6;
        border-radius: 12px;
        height: 40px;
    }
    input::placeholder {
        color: #999999;
        font-size: 12px;
    }
    .identify {
        height: 38px;
        border-radius: 12px;
        overflow: hidden;
    }
}
.input-mesCode {
    height: 48px;
}
.mesCode {
    height: 48px;
    button {
        margin-top: 7px;
        width: 100px;
        height: 38px;
        border: none;
        color: #fff;
        background-color: #1292F0;
        border-radius: 12px;
    }
    .unChoose {
        background-color: rgba(239, 239, 239, 0.3);
        color: #999999;
    }
}
.login-btn{
    width: 100%;
    margin-top: 20px;
    height: 36px;
    display: inline-block;
    padding: 0 16px;
    font-size: 14px;
    line-height: 32px;
    color: #fff;
    text-align: center;
    cursor: pointer;
    background: none;
    border: 1px solid;
    border-radius: 24px;
    border-color: #0084ff;
    background-color: #0084ff;
  }
}
.login-forget {
    width: 100%;
    margin-top: 10px;
    height: 36px;
    display: inline-block;
    padding: 0 16px;
    font-size: 14px;
    line-height: 32px;
    color: #333;
    text-align: center;
    cursor: pointer;
    background: none;
    border: 1px solid grey;
    border-radius: 24px;
}
.agreement {
    color: #737373;
    font-size: 12px;
    text-align: left;
    display: flex;
    margin-top: 12px;
    .loginAgreement {
        line-height: 20px;
    }
    .choose {
        display: inline-block;
        width: 16px;
        height: 13px;
        border: 1px solid #737373;
        margin: 3px 10px 0 0;
        line-height: 13px;
        i {
            font-weight: 700;
            color: #1292F0;
        }
    }
}
.bottom {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    line-height: 30px;
    position: absolute;
    bottom: 20px;
}
.loginTop {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 15px;
    img {
        width: 37px;
        height: auto;
    }
    .title {
        font-size: 28px;
        color: #296EB8;
        margin-left: 5px;
    }
}
</style>
